package pl.koziolekweb.szkola.LASerialNet;

// LA-SerialNet/Serwer.java
 
import java.awt.*;
import javax.swing.*;
import java.net.*;
import java.io.*;
import java.util.*;

public class Serwer extends JFrame {

    /**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	final MySerwPanel panel = new MySerwPanel();

    public static void main(String[] args) {
        Serwer serwer = new Serwer();
        serwer.runSerwer();
    }

    Serwer() {
        super("SERWER");
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        panel.setBackground(Color.BLACK);
        panel.setOpaque(true);
        panel.setPreferredSize(new Dimension(300,200));
        add(panel,BorderLayout.CENTER);
        pack();
        setVisible(true);
    }

    @SuppressWarnings("unchecked")
	void runSerwer() {
        ServerSocket        ss = null;
        Socket             soc = null;
        ObjectInputStream   in = null;
        try {
            ss = new ServerSocket(54321);
            while (true) {
                soc = ss.accept();
                in  = new ObjectInputStream(
                          soc.getInputStream());

                final ArrayList<Kreska> kreski =
                    (ArrayList<Kreska>)in.readObject();
                soc.close();

                SwingUtilities.invokeLater(new Runnable() {
                    public void run() {
                        panel.kreski.addAll(kreski);
                        panel.repaint();
                    }
                });
            }
        }catch(Exception a) {
            a.printStackTrace();
            System.exit(1);
        }
    }
}
